/**
 *  Copyright (C) 2006 zhangbo (freeeob@gmail.com)
 *
 *  This product is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation; either version 2.1 of the License, or
 *  (at your option) any later version.
 * 
 *  This product is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 * 
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
 *
 *  author:zhangbo
 *  Email:jsjava@gmail.com
 */
 
function ColorUtils(){
	this.jsjava_class="jsorg.eob.document.ColorUtils";
}

/**
 * Get the hex color form Color object
 * @param color the Color object
 */
ColorUtils.toHexValue=function(color){
	if(color==undefined){
		return "FFFFFF";
	}
	var redValue=color.getRed();
	var greenValue=color.getGreen();
	var blueValue=color.getBlue();
	return ColorUtils.toHexValueWithRGB(redValue,greenValue,blueValue);
};

/**
 * Get the hex value of color
 * @param redValue the red component
 * @param greenValue the green component
 * @param blueValue the blue component
 */
ColorUtils.toHexValueWithRGB=function(redValue,greenValue,blueValue){
	var flag1=ValidatorUtils.isShortInRange(redValue,0,255);
	var flag2=ValidatorUtils.isShortInRange(greenValue,0,255);
	var flag3=ValidatorUtils.isShortInRange(blueValue,0,255);
	if(!flag1||!flag2||!flag3){
		throw new IllegalArgumentException(IllegalArgumentException.ERROR,"Invalid rgb input!");
	}
	var redHexValue=Integer.toHexString(redValue);
	if(redHexValue.length==1){
		redHexValue="0"+redHexValue;
	}
	var greenHexValue=Integer.toHexString(greenValue);
	if(greenHexValue.length==1){
		greenHexValue="0"+greenHexValue;
	}
	var blueHexValue=Integer.toHexString(blueValue);
	if(blueHexValue.length==1){
		blueHexValue="0"+blueHexValue;
	}
	var hexValue=redHexValue+greenHexValue+blueHexValue;
	return hexValue;
};

/**
 * Create a Color object with the hex value of color
 * @param redHexValue the hex value of red component
 * @param greenHexValue the hex value of green component
 * @param blueHexValue the hex value of blue component
 */
ColorUtils.createHexColor=function(redHexValue,greenHexValue,blueHexValue){
	var redValue=parseInt("0x"+redHexValue);
	var greenValue=parseInt("0x"+greenHexValue);
	var blueValue=parseInt("0x"+blueHexValue);
	var color=new Color(redValue,greenValue,blueValue,255);
	return color;
};

/**
 * Get a palette color datum put into a (m,n) array
 * @param m the x dimensionality
 * @param n the y dimensionality
 */
ColorUtils.toPaletteDatum=function(m,n){
	function getDatum(m,n){		
		var hexColors=MultiDimensionArrayUtils.createTwoDimensionArray(m,n);
		var max=0xffffff;
		var sn=m*n;
		var span=max/sn;
		for(var i=0,s=0;i<m,s<sn;i++){
			for(var j=0;j<n;j++,s++){
				var hexValue=Integer.toHexString(s*span);
				var hexColorValue=""+hexValue;
				var zeroNum=6-hexValue.length;
				for(var k=0;k<zeroNum;k++){
					hexColorValue="0"+hexColorValue;
				}
				hexColors[i][j]=hexColorValue;
			}
		}
		return hexColors;
	}
	function getGreyDatum(m){
		var hexColors=new Array(m);
		var max=255;
		hexColors[0]="000000";
		hexColors[m-1]="ffffff";
		var span=max/m;
		for(var i=1;i<m-1;i++){
			var iv=i*span;
			hexColors[i]=new Color(iv,iv,iv).toHexValue();
		}
		return hexColors;
	}
	function getLifeDatum(m){
		var lifeColors=[Color.red,Color.pink,Color.orange,Color.yellow,Color.green,Color.magenta,Color.cyan,Color.blue];
		var ll=lifeColors.length;
		if(m>ll){
			for(var i=ll;i<m;i++){
				lifeColors[i]=Color.black;
			}
		}
		var hexColors=new Array(m);
		for(var i=0;i<m;i++){
			hexColors[i]=lifeColors[i].toHexValue();
		}
		return hexColors;
	}
	var datum=getDatum(m,n);
	var datum2=getGreyDatum(n);
	var datum3=getLifeDatum(m);
	for(var i=0;i<n;i++){
		datum[m-1][i]=datum2[i];
	}
	for(var i=0;i<m-1;i++){
		datum[i][n-1]=datum3[i];
	}
	return datum;
};